import Vue from 'vue';
import Vuex from 'vuex';
import { local } from './local/index';
import { UserModule } from './modules/user.module';
import { config as vuexConfig, getModule, VuexModule } from 'vuex-module-decorators';
import { TokenModule } from './modules/token.module';

vuexConfig.rawError = true;

Vue.use(Vuex);

const store = new Vuex.Store<{ name: string }>({
  state: {
    name: 'dasd'
  },
  mutations: {
    CHANGE_INIT_NAME(state) {
      state.name = 'init';
    }
  },
  actions: {},
  modules: {
    // User 非装饰器版本需要导入
    user: UserModule,
    token: TokenModule
  },
  plugins: [
    // local 需要使用去掉注释即可
    local
  ]
});
store.commit('CHANGE_INIT_NAME');

export default store;

type ConstructorOf<C> = {
  new (...args: any[]): C;
};

export function useStore<T extends VuexModule>(ModuleItem: ConstructorOf<T>): T {
  return getModule(ModuleItem, store);
}
